package org.p2psimulator.analysis.jfreechart;

import java.awt.Dimension;
import java.awt.HeadlessException;
import java.io.File;
import java.io.IOException;
import javax.swing.JPanel;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.Dataset;

public abstract class ChartBoard extends JPanel {

    private static final long serialVersionUID = 1L;
    JFreeChart chart;
    String title;

    public ChartBoard() throws HeadlessException {
        super();
    }

    public ChartBoard(String title, Dataset dataset, String xlabel,
            String ylabel) throws HeadlessException {

        this.title = title;
        chart = createChart(dataset, xlabel, ylabel);
        chart.setTitle(title);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(300, 300));
        add(chartPanel);
        setVisible(true);
    }

    public abstract JFreeChart createChart(Dataset dataset, String xlabel, String ylabel);

    public static void saveAsPng(String path, JFreeChart chart) throws IOException {
        ChartUtilities.saveChartAsPNG(new File(path), chart, 800, 600);
    }

    public JFreeChart getChart() {
        return chart;
    }

    public void setChart(JFreeChart chart) {
        this.chart = chart;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    
}
